/*
 * Project Name spring-boot-tools
 * File Name AutoCreatePool
 * Package Name com.huxiaosu.tools.mybatis.autocreate
 * Create Time 2024/7/11
 * Create by name：liujie
 */
package com.huxiaosu.tools.autocreate;

import com.baomidou.mybatisplus.annotation.DbType;
import com.huxiaosu.tools.autocreate.database.CreateMysql;
import com.huxiaosu.tools.autocreate.tables.CreateMysqlTable;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Description
 *
 * @author liujie
 * @date 2024/7/11 11:56
 */
public class AutoCreatePool {
    private static final Map<DbType, AutoCreateAware> registeredCreateDatabaseDao = new ConcurrentHashMap<>();
    private static final Map<DbType, AutoCreateAware> registeredCreateTableDao = new ConcurrentHashMap<>();
    private static final String TABLE = "Table";
    static {
        registeredCreateDatabaseDao.put(DbType.MYSQL,new CreateMysql());
        registeredCreateTableDao.put(DbType.MYSQL,new CreateMysqlTable());
        List<AutoCreateAware> allCreateDao = new ArrayList<>();
        ServiceLoader.load(AutoCreateAware.class).forEach(allCreateDao::add);
        allCreateDao.forEach(itemClass->{
            String name = itemClass.getClass().getName();
            String substring = name.substring(name.length() - 5);
            if(substring.equals(TABLE)){
                registeredCreateTableDao.put(itemClass.getDbType(),itemClass);
            }else{
                registeredCreateDatabaseDao.put(itemClass.getDbType(),itemClass);
            }
        });
    }
    public static AutoCreateAware getCreateDatabaseDao(DbType dbType) {
        if (!registeredCreateDatabaseDao.containsKey(dbType)) {
            throw new RuntimeException("no fount ["+dbType.name()+"] CreateDatabase");
        }
        return registeredCreateDatabaseDao.get(dbType);
    }

    public static AutoCreateAware getCreateTableDao(DbType dbType) {
        if (!registeredCreateTableDao.containsKey(dbType)) {
            throw new RuntimeException("no fount ["+dbType.name()+"] CreateTables");
        }
        return registeredCreateTableDao.get(dbType);
    }

}